package nl.windgazer.arachne.components.yql;

import nl.windgazer.arachne.services.yql.YQLConduitImp;
import nl.windgazer.arachne.services.yql.YQLLocation;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;

/**
 * 193.173.38.231, 85.113.234.28, 212.0.236.214, 195.193.167.120, 87.253.144.226
 * @author mreuring
 *
 */
public class YQLIPComparisonMap extends YQLIPLocationMap {

	//Injects and Persists
	
	//Properties and Parameters

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.PROP, name = "baseips")
	private String _baseips = "212.178.142.21, 192.87.95.10, 195.193.167.120, 82.94.199.253, 145.7.91.31, 217.149.193.192, 145.87.2.17, 195.85.146.156";

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "baseSize")
	private String _baseSize = "mid";

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "bundleSize")
	private String _bundleSize = "tiny";

	@Property(read=false,write=false)
	private YQLLocation[] _baseLocations = null;

	@Property(read=false,write=false)
	private YQLLocation[] _clusterLocations = null;

	//Components

	//Events

	//Other (check for business-logic, it should not remain here)

	@Override
	public YQLLocation[] getLocations() {
		_clusterLocations = super.getLocations();

		for (YQLLocation l:_clusterLocations) {
			l.setColor(getColor(l, getBaseLocations()));
		}

		return _clusterLocations;
	}

	@Override
	public String getSingleImgUrl() {
		final StringBuilder sb = getBaseURL()
		.append('&')
		.append(YQLConduitImp.attributes("maptype", "terrain"));

		// &markers=color:blue|label:82.95.157.143|51.8,4.6667
		for (final YQLLocation o: getLocations()) {
			sb.append('&');
			sb.append(YQLConduitImp.attributes("markers", getMarker(o, _bundleSize)));
		}
		for (final YQLLocation o: getBaseLocations()) {
			sb.append('&');
			sb.append(YQLConduitImp.attributes("markers", getMarker(o, _baseSize)));
		}

		return sb.toString();
	}

	public YQLLocation[] getBaseLocations() {
		if (_baseLocations==null) {
			_baseLocations = getLocations(_baseips);
		}
		return _baseLocations;
	}

	public void setBaseLocations(YQLLocation[] baseLocations) {
		_baseLocations = baseLocations;
	}

	public String getMarker(final YQLLocation o, final String size) {
		final StringBuilder sbv = new StringBuilder();
		sbv.append("size:")
		.append(size)
		.append("|color:")
		.append(o.getColor())
		.append("|label:")
		.append(o.getName())
		.append('|')
		.append(o.getLatitude())
		.append(',')
		.append(o.getLongitude());
		return sbv.toString();
	}

	public String getColor(YQLLocation origin, YQLLocation[] targets) {
		double distance = 100000;
		YQLLocation target = null;
		for (YQLLocation l:targets) {
			final double currentDistance = Math.sqrt(
					Math.pow(Math.abs(origin.getLatitude() - l.getLatitude()), 2) + Math.pow(Math.abs(origin.getLongitude() - l.getLongitude()), 2)
			);
			if (currentDistance < distance) {
				distance = currentDistance;
				target = l;
			}
		}
		return target!=null?target.getColor():null;
	}

}